<?php
class Application_Model_DbTable_Qcm extends Zend_Db_Table_Abstract{
	protected $_name = 'qcm';
	protected $_primary = 'id';
	
	public function fetchAllByGroup($section,$annee,$groupe){
		$select = $this->select();
		$select->setIntegrityCheck(false);
		
		$select	->from(array('q'=>$this->_name),array('*','qcm_id'=>'q.id'))
				->joinLeft(array('a'=>'attribution'), 'q.id=a.id_qcm')
				->joinLeft(array('p'=>'personne'), 'q.auteur=p.id')
				->where('a.id_section = ?',$section)
				->where('a.annee = ?',$annee)
				->where('a.groupe = ?',$groupe)
				->order('q.id asc');
		return $select->query()->fetchAll(PDO::FETCH_OBJ);
	}
	public function findWithQuestions($id){
		$questDbTable = new Application_Model_DbTable_Question();
		$select = $this->select();
		$select->setIntegrityCheck(false);
		
		$select	->from(array('q'=>$this->_name))
				->where('q.id = ?',$id);
		$result = $select->query()->fetch(PDO::FETCH_OBJ);
		$result->questions = $questDbTable->fetchAllWithReponses($id);
		return $result;
	}
	public function fetchAllByAuthorWithQuestions($author){
		$select = $this->select();
		
		$select ->from(array('q'=>'qcm'))
				->where('q.auteur = ?',$author);
				
		
		$qcmArray = $select->query()->fetchAll(PDO::FETCH_OBJ);
		
		$questDBA = new Application_Model_DbTable_Question();
		foreach ($qcmArray as $i=>$value) {
			$qcmArray[$i]->questions = $questDBA->fetchAllWithReponses($value->id);
		}
		return $qcmArray;
	}
}